package com.ywjt.platformbusiness.store.order.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import com.cy.tablayoutniubility.FragPageAdapterVp2;
import com.cy.tablayoutniubility.TabAdapter;
import com.cy.tablayoutniubility.TabLayoutScroll;
import com.cy.tablayoutniubility.TabMediatorVp2;
import com.cy.tablayoutniubility.TabViewHolder;
import com.ywjt.platformbusiness.R;
import com.ywjt.platformbusiness.base.BaseActivity;
import com.ywjt.platformbusiness.common.UserData;
import com.ywjt.platformbusiness.constants.UrlConstants;
import com.ywjt.platformbusiness.http.HttpRequest;
import com.ywjt.platformbusiness.http.HttpStringCallBack;
import com.ywjt.platformbusiness.store.model.StoreOrderDataModel;
import com.ywjt.platformbusiness.store.order.fragment.StoreOrderFragment;
import com.ywjt.platformbusiness.util.JsonUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;


public class StoreOrderList extends BaseActivity implements View.OnClickListener {
    private TabLayoutScroll tab1;
    private TextView tvTodayNew,tvTodayTotal,tvAllNew,tvAllTotal;
    private LinearLayout llSearch;
    private ViewPager2 viewpager;
    private int status=0;

    public static void actionStart(Context context) {
        Intent intent = new Intent(context, StoreOrderList.class);
        context.startActivity(intent);
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        setAndroidNativeLightStatusBar(true);
    }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.store_ordermanager_activity;
    }

    @Override
    protected void initView() {
        setTitleText("订单管理");
        tab1=findViewById(R.id.tab1);
        tvTodayNew=findViewById(R.id.tvTodayNew);
        viewpager=findViewById(R.id.viewpager);
        llSearch=findViewById(R.id.llSearch);
        tvTodayTotal=findViewById(R.id.tvTodayTotal);
        tvAllNew=findViewById(R.id.tvAllNew);
        tvAllTotal=findViewById(R.id.tvAllTotal);

        getCenterData();
        FragPageAdapterVp2<String> fragmentPageAdapter = new FragPageAdapterVp2<String>(this) {

            @Override
            public Fragment createFragment(String bean, int position) {
                switch (position) {
                    case 0://全部
                        status = -10;
                        break;
                    case 1://待付款
                        status = 0;
                        break;
                    case 2://待发货
                        status = 1;
                        break;
                    case 3://待收货
                        status = 2;
                        break;
                    case 4://完成
                        status=3;
                        break;
                    case 5:// 售后
                        status=-2;
                        break;

                }
                StoreOrderFragment orderFragment = StoreOrderFragment.newInstance(status);
                return orderFragment;
            }

            @Override
            public void bindDataToTab(TabViewHolder holder, int position, String bean, boolean isSelected) {
                TextView textView = holder.getView(R.id.tv);
                if (isSelected) {
                    textView.setTextColor(0xffFB222A);
                    textView.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                } else {
                    textView.setTextColor(0xff333333);
                    textView.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                }
                textView.setText(bean);
            }

            @Override
            public int getTabLayoutID(int position, String bean) {
                return R.layout.tab_item;
            }
        };
        TabAdapter<String> tabAdapter = new TabMediatorVp2<String>(tab1, viewpager).setAdapter(fragmentPageAdapter);
        List<String> list = new ArrayList<>();
        list.add("全部");
        list.add("待付款");
        list.add("待发货");
        list.add("待收货");
        list.add("已完成");
        list.add("售后");
        fragmentPageAdapter.add(list);
        tabAdapter.add(list);


        llSearch.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        setAndroidNativeLightStatusBar(true);
    }

    @Override
    protected int getTitleBarType() {
        return TITLEBAR_DEFAULT;
    }

    private void getCenterData() {
        new HttpRequest(getContext()).doGet(UrlConstants.merchant_getOrderDataShow+
                UserData.create(StoreOrderList.this).get(UserData.MERCHANTID), "", new HttpStringCallBack() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onSuccess(Object result) {
                try {
                    String data = result.toString();
                    //把请求得数据转换成json对象
                    JSONObject jsonObject = null;
                    jsonObject = new JSONObject(data);
                    String message = jsonObject.getString("message");
                    Boolean success=jsonObject.getBoolean("success");
                    if(success){
                        Object obj = JsonUtils.fromJson(result.toString(), StoreOrderDataModel.class);
                        if (obj instanceof StoreOrderDataModel) {
                            StoreOrderDataModel model = (StoreOrderDataModel) obj;
                            tvTodayNew.setText(model.getResult().getNewOrderNum()+"");
                            tvTodayTotal.setText(model.getResult().getTodayOrderNum()+"");
                            tvAllNew.setText(model.getResult().getAllNewOrderNum()+"");
                            tvAllTotal.setText(model.getResult().getAllOrderNum()+"");
                        }
                    }else {
                        Toast.makeText(getContext(),message,Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int code, String msg) {
                Toast.makeText(getContext(),msg,Toast.LENGTH_SHORT).show();
            }
        });
    }

    /**
     * Called when a view has been clicked.
     *
     * @param v The view that was clicked.
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.llSearch:
                //搜索订单  跳转到搜索页面
                ActivityOrderSearch.actionStart(StoreOrderList.this);
                break;
        }
    }
}
